home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / uupc11ys.zip / LIB / MKTIME2.C < prev    next >
C/C++ Source or Header  |  1992-11-27  |  2KB  |  88 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    paranoid version of mktime() for Borland C++                    */
  3. /*                                                                    */
  4. /*    Written by Gary Blaine (TeamB) and posted to CompuServe         */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. #include <stdlib.h>
  8.  #include <time.h>
  9.  #include <dos.h>
  10.  #include <mem.h>
  11.  
  12. static int   day_tab[2][12] = {
  13.    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  14.    {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  15. };
  16.  
  17. static int _Cdecl isleap(int year);
  18.  
  19. time_t _Cdecl mktime(struct tm * tm)
  20. {
  21.    struct tm   tmp;
  22.    int         temp;
  23.    int         days;
  24.    time_t      gmt_seconds;
  25.    struct date date;
  26.    struct time time;
  27.  
  28.    tzset();
  29.  
  30.    if( tm->tm_sec  < 0  ||
  31.        tm->tm_min  < 0  ||
  32.        tm->tm_hour < 0  ||
  33.        tm->tm_mday < 0  ||
  34.        tm->tm_mon  < 0  ||
  35.        tm->tm_year < 0 )
  36.       return(-1);
  37.  
  38.    tmp = *tm;
  39.    temp = tmp.tm_mon % 12;
  40.    tmp.tm_year += tmp.tm_mon / 12 + 1900;
  41.    tmp.tm_mon = temp;
  42.    if(tmp.tm_year < 0)
  43.       return(-1);
  44.    temp = tmp.tm_sec % 60;
  45.    tmp.tm_min += tmp.tm_sec / 60;
  46.    tmp.tm_sec = temp;
  47.    if(tmp.tm_min < 0)
  48.       return(-1);
  49.    temp = tmp.tm_min % 60;
  50.    tmp.tm_hour += tmp.tm_min / 60;
  51.    tmp.tm_min = temp;
  52.    if(tmp.tm_hour < 0)
  53.       return(-1);
  54.    temp = tmp.tm_hour % 24;
  55.    tmp.tm_mday += tmp.tm_hour / 24;
  56.    tmp.tm_hour = temp;
  57.    if(tmp.tm_mday < 0)
  58.       return(-1);
  59.    while(tmp.tm_mday  >  (days = day_tab[isleap(tmp.tm_year)][tmp.tm_mon]))
  60.       {
  61.       tmp.tm_mday -= days;
  62.       tmp.tm_mon++;
  63.       if(tmp.tm_mon > 11)
  64.          {
  65.          tmp.tm_mon = 0;
  66.          if(++tmp.tm_year < -1)
  67.             return(-1);
  68.          }
  69.       }
  70.    if(tmp.tm_year < 1980)
  71.       return(-1);
  72.    time.ti_hour = tmp.tm_hour;
  73.    time.ti_min = tmp.tm_min;
  74.    time.ti_sec = tmp.tm_sec;
  75.    time.ti_hund = 0;
  76.    date.da_year = tmp.tm_year;
  77.    date.da_mon = tmp.tm_mon + 1;
  78.    date.da_day = tmp.tm_mday;
  79.    gmt_seconds = dostounix(&date, &time);
  80.    memcpy( tm, localtime(&gmt_seconds), sizeof( struct tm ) );
  81.    return(gmt_seconds);
  82. }
  83.  
  84. static int _Cdecl isleap(int year)
  85. {
  86.    return(year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
  87. }
  88.